import axios from "axios"
import querystring from "querystring"
// 参考文档 axois看云
const errorHandle = (status,info)=>{
    switch(status){
    case 400:
        console.log("语义有误");
        break;
        default:
            console.log(info);
        }
            
}

const instance = axios.create({
    timeout:5000
})
// 拦截器最常用
// 发送数据之前
instance.interceptors.request.use(
    config =>{
        if(config.methods === "post"){
            config.data = querystring.stringify(config.data)
        }
        return config;
    },
    error=>{
        return Promise.reject(error)
    }
   
)
 // 获得数据之前
 instance.interceptors.response.use(
    response =>{
        return Promise.resolve(response)
    },
    error =>{
        const response = {error};
        errorHandle(response.status,response.info)
    }
 )

export default instance;